﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Brain.Editor.Properties;

namespace Brain.Editor
{
    public partial class GameFolderManager : Form
    {
        public GameFolderManager()
        {
            InitializeComponent();

            DirectoryInfo gamesDir = new DirectoryInfo(
                    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                    "Brain 2 Game Engine\\Games"));
            if (!gamesDir.Exists)
            {
                gamesDir.Create();
            }

            folder = gamesDir.FullName;

            text_GameName.Text = "Game";
            this.text_MainFolder.Text = Path.Combine(folder, text_GameName.Text);

            Test();
        }

        bool failedTest = false;
        private void Test()
        {
            if (Directory.Exists(this.text_MainFolder.Text))
            {
                failedTest = true;
                gameFolderStatus.Image = Resources.inativo;
            }
            else
            {
                failedTest = false;
                gameFolderStatus.Image = Resources.ativo;
            }
        }

        string folder = "";
        private void button_MainFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.RootFolder = Environment.SpecialFolder.MyDocuments;

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                folder = dialog.SelectedPath;
                this.text_MainFolder.Text = Path.Combine(folder, text_GameName.Text);
            }

            Test();
        }

        private void text_GameName_TextChanged(object sender, EventArgs e)
        {
            this.text_MainFolder.Text = Path.Combine(folder, text_GameName.Text);

            Test();
        }

        private void button_AddPluginExtern_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.Filter = "Brain Plugin Library files|*.dll";

            if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.list_Plugins.Items.Add(file.FileName);
            }
        }

        private void button_AddDefaultPlugin_Click(object sender, EventArgs e)
        {

        }

        private void button_RemovePlugin_Click(object sender, EventArgs e)
        {
            if (this.list_Plugins.SelectedIndex != -1)
            {
                this.list_Plugins.Items.RemoveAt(this.list_Plugins.SelectedIndex);
            }
        }

        private void button_Make_Click(object sender, EventArgs e)
        {
            if (!failedTest)
            {
                Directory.CreateDirectory(this.text_MainFolder.Text);

                string levelsFolder = Path.Combine(this.text_MainFolder.Text, "Levels");
                string contentFolder = Path.Combine(this.text_MainFolder.Text, "Content");
                string pluginsFolder = Path.Combine(this.text_MainFolder.Text, "Plugins");
                Directory.CreateDirectory(levelsFolder);
                Directory.CreateDirectory(contentFolder);
                Directory.CreateDirectory(pluginsFolder);

                for (int i = 0; i < this.list_Plugins.Items.Count; i++)
                {
                    string ob = (string)this.list_Plugins.Items[i];
                    string name = Path.GetFileName(ob);
                    File.Copy(ob, Path.Combine(pluginsFolder, name));
                }

                MessageBox.Show("Succesfully created Game Folders! Now you can make a new Level");
                this.Close();
            }
        }
    }
}
